package com.leyou.controller;

import com.leyou.pojo.Category;
import com.leyou.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.security.provider.certpath.CertId;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 根据节点id查询所有
     *
     * @param pid
     * @return
     */
    @RequestMapping("list")
  public ResponseEntity<List<Category>>queryCategory(@RequestParam("pid")Integer pid) {
        if (pid == null || pid.longValue() < 0) {
            return ResponseEntity.badRequest().build();
        }
        List<Category> categories = this.categoryService.queryCategoriesByPid(pid);

        if (CollectionUtils.isEmpty(categories)) {
            // 响应404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categories);

    }

    //删除
    @RequestMapping("deleteById")
    public  ResponseEntity<String>deleteById(@RequestParam("id")Integer id){
       int i=this.categoryService.deleteById(id);
       String result="";
       if (i==0){
           result="Fail";
       }else {
           result="SUCESS";
       }
        return ResponseEntity.ok(result);
    }
    //修改
    @RequestMapping("update")
    public  ResponseEntity<String>updateCategory(@RequestBody Category category){
       int i=this.categoryService.updateCategory(category);
       String result="";
       if (i==0){
           result="Fail";
       }else {
           result="SUCESS";
       }
        return ResponseEntity.ok(result);
    }
    //修改
    @RequestMapping("add")
    public  ResponseEntity<String>addCategory(@RequestBody Category category){
       int i=this.categoryService.addCategory(category);
       String result="";
       if (i==0){
           result="Fail";
       }else {
           result="SUCESS";
       }
        return ResponseEntity.ok(result);
    }

   //通过分类id查询分类
    @RequestMapping("findCategoryById/{cid}")
    public Category findCategoryById(@RequestParam("id") Integer cid){
        Category category= categoryService.findCategoryById(cid);
        return category;
    }



    //通过ids获取对应的分类
    @RequestMapping("findCategoryByCids")
     public List<Category> findCategoryByCids(@RequestParam("cid1") Integer cid1,@RequestParam("cid2") Integer cid2,@RequestParam("cid3") Integer cid3){
        List<Category> list=categoryService.findCategoryByCids(cid1,cid2,cid3);
       return list;
     }


}
